﻿/* $RCSFile: DemoMark.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/UI/WebControls/DemoMark.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:29  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Globalization;

using AddonNice.Diagnostics;
using AddonNice.Settings;
using AddonNice.Core;
using AddonNice.UI;
using AddonNice.UI.WebControls;
using AddonNice.Helpers;
using AddonNice.Security;
using AddonNice.Configuration;

using Trace2    =   System.Diagnostics.Trace;

namespace AddonNice.UI.WebControls
{
    /// <summary>
    /// If the page is a demo page write a demo text inside a Div tag, the TextFormat property allows to include any text 
    /// It must contain a {0} to be replaced by the localized 'Demo' text. 
    /// </summary>
    public class DemoMark: WebControl
	{
		/// <summary>
		/// If true demo Info from license is returned
        /// Default false
		/// </summary>
        public bool LicenseInfo
		{
			get
			{
                object ob   =   ViewState["LicenseInfo"];
				if ( ob != null )
					return (bool)ob;
				return false;
			}
			set
			{
                ViewState["LicenseInfo"] =   value;
			}
		}

        /// <summary>
        /// Text format to display in demo mode
        /// If contains {0}, will be used as a format to display localized 'Demo' or detailled license Info
        /// if empty displays localized 'Demo'
        /// </summary>
		public string TextFormat
		{
			get
			{
                object ob   =   ViewState["TextFormat"];
				if ( ob != null )
					return (string)ob;
				return string.Empty;
			}
			set
			{
                ViewState["TextFormat"] =   value;
			}
		}
        public DemoMark() : base(HtmlTextWriterTag.Div)
		{
			EnableViewState = false;
		}

        protected override void RenderContents(HtmlTextWriter writer)
        {
            if (WebControlsTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]DemoMark RenderContents BEG LicenseInfo: {0}.",LicenseInfo),WebControlsTraceSwitch.Sw.Info);
            string str  =   LocalizeHelper.Localize("DISPLAY_DEMO","Demo");
            if ( LicenseInfo && ( Page is ADNPage) )
                str     =   ((ADNPage)Page).DemoInfo;
            string fmt  =   TextFormat;
            if (( fmt.IndexOf("{0") > -1 ) && (fmt.IndexOf("{1") == -1 ) )
                str     =   string.Format(fmt,str);
            else if ( !string.IsNullOrEmpty(fmt) )
                str     =   fmt;
            writer.Write(str);
            if (WebControlsTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]DemoMark RenderContents END str: {0}.",str),WebControlsTraceSwitch.Sw.Info);
        }

		protected override void OnInit(EventArgs e)
		{
            ADNPage pg      =   Page as ADNPage;
            if (WebControlsTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]DemoMark OnInit BEG ADNPage: {0}.",(pg != null)),WebControlsTraceSwitch.Sw.Info);
            if ( pg != null && pg.IsDemo )
                Visible     =   true;
            else Visible    =   false;
            base.OnInit(e);
            if (WebControlsTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]DemoMark OnInit END Visible: {0}.",Visible),WebControlsTraceSwitch.Sw.Info);
        }

    }
}